package com.xunware.article.action.admin;

import static com.xunware.cms.util.Constants.ARTICLE_SYS;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import net.sf.json.JsonConfig;
import com.project.common.hqlUtil.Pagination;
import com.project.common.server.BaseServer;
import com.xunware.article.prototype.Article;
import com.xunware.article.server.ArticleServ;
import com.xunware.cms.prototype.ChnlModel;
import com.xunware.cms.prototype.CmsAdmin;
import com.xunware.sys.prototype.Admin;
import com.xunware.cms.prototype.CmsChannel;
import com.xunware.cms.server.ChnlModelServ;
import com.xunware.cms.server.CmsChannelServ;
import com.xunware.cms.util.Constants;
import com.xunware.sys.action.base.JcmsServAction;
import com.xunware.util.ExtHelper;
import com.xunware.util.JsonFilter;
import com.xunware.util.PaginationSupport;

/*
 * 作者： 廖建勋
 * 栏目操作 Action
 * 
 */
public class ChannelAction extends JcmsServAction{

	private static final long serialVersionUID = 8492155778546793419L;
	
	protected String getSysType() {
		return ARTICLE_SYS;
	}
	
	/**
	 * 链接至首页
	 * @return
	 */
	public String linkIndex(){
		return "indexChan";
	}
		
	
	/**
	 * 主要是为了在IE中能正常显示树，而做的字符串截取
	 */
	public StringBuffer formatChar(StringBuffer tree,boolean flag){
		   if(flag){
	            int num = (tree.lastIndexOf(","));
		        tree.replace(num,num+1, "");
		        tree.append(" ] }, ");
		   }else{
			    tree.append(" ] },");
		   }
		   return tree;
	}
	
	/**
	 *  二级栏目处理类
	 */
	public StringBuffer twoChannel(StringBuffer tree,boolean flag){
		   if(flag){
	            int num = (tree.lastIndexOf(","));
		        tree.replace(num,num+1, "");
		        tree.append(" ] } ] ");
		   }else{
			    tree.append(" ] } ]");
		   }
	       return tree;	
	}
	
	/**
	 *  父栏目节点树
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String parentChanTreeList(){
		 Long webId = this.getExtWebId();
		 CmsChannel mainChan = cmsChannelServ.getRoot("article", webId);    //一级
		 StringBuffer tree = new StringBuffer();		 
         if(mainChan != null ){          	  
     	      Set<CmsChannel> twoChAll = mainChan.getChild();         //站点下的二级栏目 
     	     
     	      tree.append(" [ ");      	      
     	      if ( twoChAll.size()<=0 ){       //表示主栏目下为空
     	    	   tree.append("{id:'"+mainChan.getId()+"',text:'"+mainChan.getName()+"',leaf:true,url:'../admin/article/channel/Com_channel.do?node="+mainChan.getId()+"'} ] ");
     	    	   this.message = tree.toString();
     	    	   return INFOWRAP;
     	      }else{     	    	   
     	           tree.append("{id:'"+mainChan.getId()+"',text:'"+mainChan.getName()+"',leaf:false,url:'../admin/article/channel/Com_channel.do?node="+mainChan.getId()+"', children:[");
     	      }   
     	      
     	      Iterator twoIt = twoChAll.iterator();  
     	      boolean twoflag = false;                                      //二级栏目是否为空标记
     	      while(twoIt.hasNext()){
     	    	    CmsChannel twoCh = (CmsChannel)twoIt.next();      	    	   
     	    	    if(twoCh != null ){ //管理员ID中存在二级栏目
     	    	    	   twoflag = true;
						   Set<CmsChannel> threeSet =  twoCh.getChild(); 						    	   
				    	   if(threeSet != null && threeSet.size()>0){
				    		       boolean threeflag = false;                //三级栏目是否为空标记
			   			    	   tree.append("{ id:'"+twoCh.getId()+"',text:'"+twoCh.getName()+"',leaf:false,url:'../admin/article/channel/Com_channel.do?node="+twoCh.getId()+"',children:[ ");
			   			    	   Iterator threeIt =  threeSet.iterator();			   			    	   
			   			    	    while(threeIt.hasNext()){
			   			    		      CmsChannel threeChan = (CmsChannel)threeIt.next();			   			    		   
			   			    		      if( threeChan != null ){ //三级栏目
			   			    		    	     threeflag = true;
				   			    		         Set<CmsChannel> fourSet = threeChan.getChild();      
				   			    		      if(fourSet != null && fourSet.size()>0){//四级栏目
			   			    		    	        boolean fourflag = false;
			   			    		    	        tree.append("{id:'"+threeChan.getId()+"',text:'"+threeChan.getName()+"',leaf:false,url:'../admin/article/channel/Com_channel.do?node="+threeChan.getId()+"',children:[ ");
			   			    		    	        Iterator fourIt =  fourSet.iterator();				   			    		    	        
			   			    		    	        while(fourIt.hasNext()){
			   			    		    		         CmsChannel four = (CmsChannel)fourIt.next();				   			    		    		         
			   			    		    		         if(four != null ){	
			   			    		    		        	fourflag = true;
			   			    		    		        	Set<CmsChannel> fiveSet = four.getChild();
			   			    		    		        	if (fiveSet != null && fiveSet.size()>0){				   			    		    		        	    
			   			    		    		        		 boolean fiveflag = false;
			   			    		    		        		 tree.append("{id:'"+four.getId()+"',text:'"+four.getName()+"',leaf:false,url:'../admin/article/channel/Com_channel.do?node="+four.getId()+"',children:[ ");
			   			    		    		        		 Iterator fiveIt =  fiveSet.iterator();
			   			    		    		        		 while(fiveIt.hasNext()){
			   			    		    		        			  CmsChannel five = (CmsChannel)fiveIt.next();
			   			    		    		        			  if(five != null ){
			   			    		    		        				   fiveflag = true;
			   			    		    		        				   Set<CmsChannel> sexSet = five.getChild();
			   			    		    		        				   if (sexSet != null && sexSet.size()>0){	
			   			    		    		        					   boolean sexflag = false;
			   			    		    		        					   tree.append("{id:'"+five.getId()+"',text:'"+five.getName()+"',leaf:false,url:'../admin/article/channel/Com_channel.do?node="+five.getId()+"',children:[ ");
			   			    		    		        					   Iterator sexIt =  sexSet.iterator();
			   			    		    		        					   while(sexIt.hasNext()){
						   			    		    		        			  CmsChannel sex = (CmsChannel)sexIt.next();
						   			    		    		        			  if(sex != null){
						   			    		    		        				  sexflag = true;
						   			    		    		        				  Set<CmsChannel> sevenSet = sex.getChild();
						   			    		    		        				  if (sevenSet != null && sevenSet.size()>0){
						   			    		    		        					  boolean sevenflag = false;
						   			    		    		        					  tree.append("{id:'"+sex.getId()+"',text:'"+sex.getName()+"',leaf:false,url:'../admin/article/channel/Com_channel.do?node="+sex.getId()+"',children:[ ");
						   			    		    		        					  Iterator sevenIt =  sevenSet.iterator();
						   			    		    		        					  while(sevenIt.hasNext()){
									   			    		    		        			  CmsChannel eight = (CmsChannel)sevenIt.next();
									   			    		    		        			  if(eight != null ){
									   			    		    		        				  sevenflag = true;
									   			    		    		        				  tree.append("{id:'"+eight.getId()+"',text:'"+eight.getName()+"',leaf:true,url:'../admin/article/channel/Com_channel.do?node="+eight.getId()+"'}, ");
									   			    		    		        			  }
									   			    		    		        		  }
									   			    		    		        		  tree = formatChar(tree,sevenflag);
						   			    		    		        				  }else{
										   			    		    		        		 tree.append("{id:'"+sex.getId()+"',text:'"+sex.getName()+"',leaf:true,url:'../admin/article/channel/Com_channel.do?node="+sex.getId()+"'}, "); 
						   			    		    		        				  }
						   			    		    		        			  }
			   			    		    		        					   }
			   			    		    		        					   tree = formatChar(tree,sexflag);
			   			    		    		        				   
			   			    		    		        				   }else{
							   			    		    		        		 tree.append("{id:'"+five.getId()+"',text:'"+five.getName()+"',leaf:true,url:'../admin/article/channel/Com_channel.do?node="+five.getId()+"'}, "); 
			   			    		    		        				   }
			   			    		    		        			  }
			   			    		    		        		 }
			   			    		    		        		 tree = formatChar(tree,fiveflag);
			   			    		    		        	}else{
			   			    		    		        		 tree.append("{id:'"+four.getId()+"',text:'"+four.getName()+"',leaf:true,url:'../admin/article/channel/Com_channel.do?node="+four.getId()+"'}, "); 
			   			    		    		        	}
			   			    		    		         }						   			    		    		   
			   			    		    	        }									   			          
						   			                tree = formatChar(tree,fourflag);
			   			    		          }else{
			   			    		              tree.append("{id:'"+threeChan.getId()+"',text:'"+threeChan.getName()+"',leaf:true,url:'../admin/article/channel/Com_channel.do?node="+threeChan.getId()+"'}, "); 
			   			    		          }		
			   			    		       }				   			    		    
			   			    	    }
			   			    	   tree = formatChar(tree,threeflag);
			   			    	
				    	   }else{							    		   
				    		   tree.append("{id:'"+twoCh.getId()+"',text:'"+twoCh.getName()+"',leaf:true,url:'../admin/article/channel/Com_channel.do?node="+twoCh.getId()+"'}, "); 
				    	   } 	
     	    	   }
     	       }
     	       tree = twoChannel(tree,twoflag);     	      
         }else{
        	 this.message = "";
        	 return INFOWRAP;
         }         
         this.message = tree.toString();         
		 return  INFOWRAP;
	}

	
	
	/**
	 * 站点栏目树列表
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String chanTreeList(){
		 Long webId = this.getExtWebId();
		 CmsChannel mainChan = cmsChannelServ.getRoot("article", webId);    //一级
		 StringBuffer tree = new StringBuffer();		 
         if(mainChan != null ){          	  
     	      Set<CmsChannel> twoChAll = mainChan.getChild();         //站点下的二级栏目 
     	     
     	      tree.append(" [ ");      	      
     	      if ( twoChAll.size()<=0 ){       //表示主栏目下为空
     	    	   tree.append("{id:'"+mainChan.getId()+"',text:'"+mainChan.getName()+"',checked:false,leaf:true,url:'../admin/article/channel/Com_channel.do?node="+mainChan.getId()+"'} ] ");
     	    	   this.message = tree.toString();
     	    	   return INFOWRAP;
     	      }else{     	    	   
     	           tree.append("{id:'"+mainChan.getId()+"',text:'"+mainChan.getName()+"',checked:false,leaf:false,url:'../admin/article/channel/Com_channel.do?node="+mainChan.getId()+"', children:[");
     	      }   
     	      
     	      Iterator twoIt = twoChAll.iterator();  
     	      boolean twoflag = false;                                      //二级栏目是否为空标记
     	      while(twoIt.hasNext()){
     	    	    CmsChannel twoCh = (CmsChannel)twoIt.next();      	    	   
     	    	    if(twoCh != null ){ //管理员ID中存在二级栏目
     	    	    	   twoflag = true;
						   Set<CmsChannel> threeSet =  twoCh.getChild(); 						    	   
				    	   if(threeSet != null && threeSet.size()>0){
				    		       boolean threeflag = false;                //三级栏目是否为空标记
			   			    	   tree.append("{ id:'"+twoCh.getId()+"',text:'"+twoCh.getName()+"',checked:false,leaf:false,url:'../admin/article/channel/Com_channel.do?node="+twoCh.getId()+"',children:[ ");
			   			    	   Iterator threeIt =  threeSet.iterator();
			   			    	   
			   			    	    while(threeIt.hasNext()){
			   			    		      CmsChannel threeChan = (CmsChannel)threeIt.next();			   			    		   
			   			    		      if( threeChan != null ){ //三级栏目
			   			    		    	     threeflag = true;
				   			    		         Set<CmsChannel> fourSet = threeChan.getChild();      
				   			    		         if(fourSet != null && fourSet.size()>0){//四级栏目
				   			    		    	        boolean fourflag = false;
				   			    		    	        tree.append("{id:'"+threeChan.getId()+"',text:'"+threeChan.getName()+"',checked:false,leaf:false,url:'../admin/article/channel/Com_channel.do?node="+threeChan.getId()+"',children:[ ");
				   			    		    	        Iterator fourIt =  fourSet.iterator();				   			    		    	        
				   			    		    	        while(fourIt.hasNext()){
				   			    		    		         CmsChannel four = (CmsChannel)fourIt.next();				   			    		    		         
				   			    		    		         if(four != null ){	
				   			    		    		        	fourflag = true;
				   			    		    		        	Set<CmsChannel> fiveSet = four.getChild();
				   			    		    		        	if (fiveSet != null && fiveSet.size()>0){				   			    		    		        	    
				   			    		    		        		 boolean fiveflag = false;
				   			    		    		        		 tree.append("{id:'"+four.getId()+"',text:'"+four.getName()+"',checked:false,leaf:false,url:'../admin/article/channel/Com_channel.do?node="+four.getId()+"',children:[ ");
				   			    		    		        		 Iterator fiveIt =  fiveSet.iterator();
				   			    		    		        		 while(fiveIt.hasNext()){
				   			    		    		        			  CmsChannel five = (CmsChannel)fiveIt.next();
				   			    		    		        			  if(five != null ){
				   			    		    		        				   fiveflag = true;
				   			    		    		        				   Set<CmsChannel> sexSet = five.getChild();
				   			    		    		        				   if (sexSet != null && sexSet.size()>0){	
				   			    		    		        					   boolean sexflag = false;
				   			    		    		        					   tree.append("{id:'"+five.getId()+"',text:'"+five.getName()+"',checked:false,leaf:false,url:'../admin/article/channel/Com_channel.do?node="+five.getId()+"',children:[ ");
				   			    		    		        					   Iterator sexIt =  sexSet.iterator();
				   			    		    		        					   while(sexIt.hasNext()){
							   			    		    		        			  CmsChannel sex = (CmsChannel)sexIt.next();
							   			    		    		        			  if(sex != null){
							   			    		    		        				  sexflag = true;
							   			    		    		        				  Set<CmsChannel> sevenSet = sex.getChild();
							   			    		    		        				  if (sevenSet != null && sevenSet.size()>0){
							   			    		    		        					  boolean sevenflag = false;
							   			    		    		        					  tree.append("{id:'"+sex.getId()+"',text:'"+sex.getName()+"',checked:false,leaf:false,url:'../admin/article/channel/Com_channel.do?node="+sex.getId()+"',children:[ ");
							   			    		    		        					  Iterator sevenIt =  sevenSet.iterator();
							   			    		    		        					  while(sevenIt.hasNext()){
										   			    		    		        			  CmsChannel eight = (CmsChannel)sevenIt.next();
										   			    		    		        			  if(eight != null ){
										   			    		    		        				  sevenflag = true;
										   			    		    		        				  tree.append("{id:'"+eight.getId()+"',text:'"+eight.getName()+"',checked:false,leaf:true,url:'../admin/article/channel/Com_channel.do?node="+eight.getId()+"'}, ");
										   			    		    		        			  }
										   			    		    		        		  }
										   			    		    		        		  tree = formatChar(tree,sevenflag);
							   			    		    		        				  }else{
											   			    		    		        		 tree.append("{id:'"+sex.getId()+"',text:'"+sex.getName()+"',checked:false,leaf:true,url:'../admin/article/channel/Com_channel.do?node="+sex.getId()+"'}, "); 
							   			    		    		        				  }
							   			    		    		        			  }
				   			    		    		        					   }
				   			    		    		        					   tree = formatChar(tree,sexflag);
				   			    		    		        				   
				   			    		    		        				   }else{
								   			    		    		        		 tree.append("{id:'"+five.getId()+"',text:'"+five.getName()+"',checked:false,leaf:true,url:'../admin/article/channel/Com_channel.do?node="+five.getId()+"'}, "); 
				   			    		    		        				   }
				   			    		    		        			  }
				   			    		    		        		 }
				   			    		    		        		 tree = formatChar(tree,fiveflag);
				   			    		    		        	}else{
				   			    		    		        		 tree.append("{id:'"+four.getId()+"',text:'"+four.getName()+"',checked:false,leaf:true,url:'../admin/article/channel/Com_channel.do?node="+four.getId()+"'}, "); 
				   			    		    		        	}
				   			    		    		         }						   			    		    		   
				   			    		    	        }									   			          
							   			                tree = formatChar(tree,fourflag);
				   			    		          }else{
				   			    		              tree.append("{id:'"+threeChan.getId()+"',text:'"+threeChan.getName()+"',checked:false,leaf:true,url:'../admin/article/channel/Com_channel.do?node="+threeChan.getId()+"'}, "); 
				   			    		          }	
			   			    		       }				   			    		    
			   			    	    }
			   			    	   tree = formatChar(tree,threeflag);
			   			    	
				    	   }else{							    		   
				    		   tree.append("{id:'"+twoCh.getId()+"',text:'"+twoCh.getName()+"',checked:false,leaf:true,url:'../admin/article/channel/Com_channel.do?node="+twoCh.getId()+"'}, "); 
				    	   } 	
     	    	   }
     	       }
     	       tree = twoChannel(tree,twoflag);     	      
         }else{
        	 this.message = "";
        	 return INFOWRAP;
         }         
         this.message = tree.toString();         
		 return  INFOWRAP;
	}
	
	/**
	 *  set对象中取ID，组装成Long集合
	 */
	public Set<Long> getChanSet(Set<CmsChannel> cmsChs){
		   Set<Long> cmsChIds =  new HashSet<Long>();
		   for(CmsChannel cmsCh:cmsChs){
			   cmsChIds.add(cmsCh.getId());
		   }
		   return cmsChIds;
	}
	
    /**
     *  链接至栏目显示页
     */
	@SuppressWarnings("unused")
	public String channel(){
		 if (this.node.equals("root")){
		     context.setRequestAttr("node",this.node);
		     return "indexChan";
		 }else{
		     context.setRequestAttr("node",this.node); 
		     CmsChannel cmCh = cmsChannelServ.get(new Long(this.node));
		     CmsChannel parent = cmCh.getParent();
		     Long parentId = null;
		     if(parent != null){
		    	 parentId = parent.getId();
		     }else{
		    	 parentId = 1L;
		     }
		     context.setRequestAttr("parentId",parentId);
		     context.setRequestAttr("channelName",cmCh.getName()); 
		 }
		 return "chan";
	}	
	
	/**
	 *  管理员可操作的栏目
	 */
	@SuppressWarnings("unchecked")
	public String adminChanList(){
		 Long adminId = (Long)context.getSessionAttr(Admin.ADMIN_KEY);
		 CmsAdmin cmsAd =  cmsAdminServ.get(adminId); 
	     Set<CmsChannel> twoSet = cmsAd.getChannels();           //管理员可以管理的栏目
	     Set<Long> twoAdminId = getChanSet(twoSet);              //管理员可以管理的栏目ID
	     List<CmsChannel> chList = new ArrayList();
	     if(adminId == 1){
		     if( this.node.equals("root")){
		    	   CmsChannel cmsCh = cmsChannelServ.getRoot("article", this.getExtWebId());
		    	   StringBuffer str = new StringBuffer();
		    	   str.append(" { \"items\": [");	    	 
		    	   if(cmsCh != null){
		    		   String beStr = ExtHelper.getJsonFromBean(cmsCh);
		    		   str.append(beStr);
		    	   }	    	   
		    	   str.append(" ],\"results:\": 1} ");
		    	   this.message = str.toString();	    	  
		     }else {
		    	 if (this.node != null){	     
				    CmsChannel cmCh = cmsChannelServ.get(new Long(this.node));
				    Set<CmsChannel> cmsSet = cmCh.getChild();			 
				    Iterator it = cmsSet.iterator();
				    while(it.hasNext()){
					     CmsChannel chan =  (CmsChannel)it.next();
						      chList.add(chan);
				    }
			      }	
		    	  this.message = ExtHelper.getJsonFromList(chList.size(),chList);
		     }	
	     }else{
		     if( this.node.equals("root")){
		    	   CmsChannel cmsCh = cmsChannelServ.getRoot("article", this.getExtWebId());
		    	   StringBuffer str = new StringBuffer();
		    	   str.append(" { \"items\": [");	    	 
		    	   if(cmsCh != null){
		    		   String beStr = ExtHelper.getJsonFromBean(cmsCh);
		    		   str.append(beStr);
		    	   }	    	   
		    	   str.append(" ],\"results:\": 1} ");
		    	   this.message = str.toString();	    	  
		     }else {
		    	 if (this.node != null){	     
				    CmsChannel cmCh = cmsChannelServ.get(new Long(this.node));
				    Set<CmsChannel> cmsSet = cmCh.getChild();			 
				    Iterator it = cmsSet.iterator();
				    while(it.hasNext()){
					     CmsChannel chan =  (CmsChannel)it.next();
					     if(chan != null && twoAdminId.contains(chan.getId())){
						      chList.add(chan);
					      }				   
				    }
			      }	
		    	  this.message = ExtHelper.getJsonFromList(chList.size(),chList);
		     }	
	     }
			
		 return  INFOWRAP;
	}
	
	 /**
	  *  栏目分页显示
	  */
	@SuppressWarnings("unchecked")
	public String channelList()throws Exception{
		Pagination pagi = cmsChannelServ.getChannelByWebId(Constants.ARTICLE_SYS, this.getExtWebId(),this.start,100);
        List list =  pagi.getList();
		long chanNum = pagi.getTotalSize();
		this.message = ExtHelper.getJsonFromList(chanNum,list);			
		return  INFOWRAP;
	}
	
	/**
	 *  当前站点的栏目模型
	 */
	public String channelModelList()throws Exception{
		List<ChnlModel>  modelList = chnlModelServ.getModels(Constants.ARTICLE_SYS, this.getExtWebId());
		long infoNum = modelList.size();			 
		this.message = ExtHelper.getJsonFromList(infoNum,modelList);		 
		return INFOWRAP;
	}
	
	/**
	 *  属于栏目模型下的栏目
	 */
	public String modelDownChannel()throws Exception{
		 List<CmsChannel>  chans = cmsChannelServ.modelDownChannel(Constants.ARTICLE_SYS, this.getExtWebId(),this.modelId);
		 List<CmsChannel> indexCh = cmsChannelServ.getRoots(Constants.ARTICLE_SYS, this.getExtWebId());  //加上首页
		 if (indexCh != null && indexCh.size()>0){
			 CmsChannel ch = indexCh.get(0);
			 if (ch != null){
				 chans.add(ch);
			 }
		 }
		 int chanNum = chans.size();
		 this.message = ExtHelper.getJsonFromList(chanNum,chans);		 
		 return INFOWRAP;
	}
	
	/**
	 *  新增栏目
	 */
	public String findChannelById()throws Exception{
		 CmsChannel chan = cmsChannelServ.get(this.getId());
	     String[] strs = {"website","child","model","parent"};
	     JsonConfig config = JsonFilter.getFilter(strs);	
		 if (chan != null){
			  long modelId =  chan.getModel().getId();
      	      StringBuffer chanStr = new StringBuffer();
    	      chanStr.append("{success:true,data:{");
       	      String chstr = ExtHelper.getJsonFromBean(chan, config);
    	      int online = chstr.lastIndexOf("}");
    	      chanStr.append(chstr.substring(1,online));
    	      chanStr.append(",\"modelId\":"+modelId);
    	      CmsChannel parent = chan.getParent();
    	      if (parent != null){
    	    	  String chName = parent.getName();
    	    	  Long parentId = parent.getId();
    	  	      chanStr.append(",\"parentName\":'"+chName+"'");
        	      chanStr.append(",\"parentId\":"+parentId+" } }");
    	      }else{
    	    	  chanStr.append(" }}");
    	      }     	      
    	      this.message = chanStr.toString(); 
		 }else{
			  this.message = "{success:false}";
		 }		 
		 return INFOWRAP;
	}
	
	/**
	 *  更新栏目
	 */
	public String updateChannel()throws Exception{
		 bean = cmsChannelServ.get(this.getId());
		 if (bean != null){
			 setBean();
			 cmsChannelServ.update(bean);
			 this.message = "{success:true}";
		 }else{
			 this.message = "{success:false}";
		 }
		 return INFOWRAP;
	}
	
	/**
	 *  删除栏目
	 */
	public String deleteChannelById()throws Exception{
		 CmsChannel chan= cmsChannelServ.get(this.getId());
		 
		 
		 if (chan != null){	
			 List<Article> list=articleServ.selectByChannelId(chan.getId());
			 if(list.size() > 0 )
			 {
				 this.message = "{success:false,msg:'请先删除栏目下的文章!'}";				
			 }else
			 {
				 cmsChannelServ.delete(chan);
				 this.message = "{success:true}";
			 }
		 }else{
			 this.message = "{success:false,msg:'栏目删除失败!'}";
		 }		 
		 return INFOWRAP;
	}
	
	/**
	 * 清空栏目下面的所有子栏目
	 */
	public String deleteAllChanalById(){
		CmsChannel chan= cmsChannelServ.get(this.getId());
		Set<Long> chanIds = chan.getChildIdsTwo();
	    for(Long chanId: chanIds){
				 List<Article> list=articleServ.selectByChannelId(chanId);
				 if(list.size() > 0 )
				 {
					 this.message = "{success:false,msg:'请先删除栏目下的文章!'}";				
				 }else
				 {
					 cmsChannelServ.delete(cmsChannelServ.get(chanId));
					 this.message = "{success:true}";
				 }
	    }
		
		return INFOWRAP;
	}
	
	

	/**
	 *  新增栏目
	 */
	public String saveChannel()throws Exception{
		if(this.getParentId() == null){
			 this.message = "{success:false}";
			 return INFOWRAP;
		}
		 Long adminId = (Long)context.getSessionAttr(Admin.ADMIN_KEY);
		 CmsAdmin cmsAd =  cmsAdminServ.get(adminId);
		 Set<CmsAdmin> admins =  new HashSet<CmsAdmin>();
		 admins.add(cmsAd);
		 bean = new CmsChannel();
		 setUpdateBean();                               //封装值
		 bean.setVisitToday(new Long(0));
		 bean.setVisitTotal(new Long(0));
		 bean.setStatDate(new Date());	
		 bean.setAdmins(admins);
		 cmsChannelServ.saveOrUpdate(bean);
		 cmsChannelServ.clear();
		 this.message = "{success:true,msg:'成功!'}";
		 return INFOWRAP;
	}
	
	/**
	 *  新增栏目
	 */
	public String saveIndexChannel()throws Exception{
		 if(this.getParentId() == null)
		 {
			 this.message = "{success:false}";
			 return INFOWRAP;
		 }
		 Long adminId = (Long)context.getSessionAttr(Admin.ADMIN_KEY);
		 CmsAdmin cmsAd =  cmsAdminServ.get(adminId);
		 Set<CmsAdmin> admins =  new HashSet<CmsAdmin>();
		 admins.add(cmsAd);
		 bean = new CmsChannel();
		 setIndexBean();                               //封装值
		 bean.setVisitToday(new Long(0));
		 bean.setVisitTotal(new Long(0));
		 bean.setStatDate(new Date());	
		 bean.setAdmins(admins);
		 cmsChannelServ.saveOrUpdate(bean);		
		 this.message = "{success:true,msg:'首页新增成功!'}";
		 return INFOWRAP;
	}
	
	/**
	 *  更新栏目
	 */
	public String updateIndexChannel()throws Exception{
		 bean = cmsChannelServ.get(this.getId());
		 if (bean != null){
			 setIndexBean();
			 cmsChannelServ.update(bean);
			 this.message = "{success:true}";
		 }else{
			 this.message = "{success:false}";
		 }
		 return INFOWRAP;
	}

	
	/**
	 * 清空栏目下的文章
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String emptyChannel(){		
		if (this.getId() != null){
			PaginationSupport pagination =  articleServ.getPage(this.getId(),1,null,null,null,null,null,null,null, 20,0L);
			Iterator it = pagination.getList().iterator();
			while(it.hasNext()){
				  Article arti =  (Article)it.next();
				  if (arti != null){
					  articleServ.delete(arti);
				  }
			}
		}
		this.message = "{success:true,msg:'清空成功!'}";
		return INFOWRAP;
	}
	
	/**
	 *  封装值
	 */
	private void setBean(){		
		ChnlModel chModel = chnlModelServ.get(this.modelId);
		System.out.println(this.getParentId());
		if(this.getParentId() != null)
		{
			if(this.getParentId() != 0L){
				 CmsChannel parent = cmsChannelServ.get(this.getParentId());
				 if (parent != null){
					 bean.setParent(parent);
				 }
			}else{
				 CmsChannel parent = cmsChannelServ.getRoot(Constants.ARTICLE_SYS, this.getExtWebId());
				 if (parent != null){
					 bean.setParent(parent);
				 }
			}	
		}
		bean.setModel(chModel);		
		bean.setWebsite(this.getWeb());
		bean.setSysType(Constants.ARTICLE_SYS);
		bean.setName(this.name);
		bean.setPath(this.path);
		bean.setTitle(this.title);
		bean.setKeywords(this.keywords);
		bean.setDescription(this.description);
		bean.setPriority(this.priority);
		bean.setContributeLevel(this.contributeLevel);
        bean.setDisplay(this.display); 
		bean.setMainshow(this.mainshow);
		bean.setTplIndex(this.tplIndex);
		bean.setTplContent(this.tplContent);
		bean.setHasChild(false);
		bean.setParam1(this.param1);
		bean.setParam2(this.param2);
		bean.setParam3(this.param3);	
		bean.setOuterUrl(this.outerUrl);		
	}
	
	@SuppressWarnings("unused")
	private void setUpdateBean(){		
		ChnlModel chModel = chnlModelServ.get(this.modelId);
		if(this.getParentId() != null){
			 CmsChannel parent = cmsChannelServ.get(this.getParentId());
			 if (parent != null){
				 bean.setParent(parent);
			 }		
		}
		bean.setModel(chModel);		
		bean.setWebsite(this.getAdminWebSite());
		bean.setSysType(Constants.ARTICLE_SYS);
		bean.setName(this.name);
		bean.setPath(this.path);
		bean.setTitle(this.title);
		bean.setKeywords(this.keywords);
		bean.setDescription(this.description);
		bean.setPriority(this.priority);
		bean.setContributeLevel(this.contributeLevel);
        bean.setDisplay(this.display); 
		bean.setMainshow(this.mainshow);
		bean.setTplIndex(this.tplIndex);
		bean.setTplContent(this.tplContent);
		bean.setHasChild(false);
		bean.setParam1(this.param1);
		bean.setParam2(this.param2);
		bean.setParam3(this.param3);	
		if(this.outerUrl != null){
			bean.setOuterUrl(this.outerUrl);	
		}
	}
	
	/**
	 * 设置首页栏目bean
	 */
	private void setIndexBean(){
		ChnlModel chModel = chnlModelServ.get(this.modelId);
    	bean.setParent(null);		
		bean.setModel(chModel);		
		bean.setWebsite(this.getWeb());
		bean.setSysType(Constants.ARTICLE_SYS);
		bean.setName(this.name);
		bean.setPath(this.path);
		bean.setTitle(this.title);
		bean.setKeywords(this.keywords);
		bean.setDescription(this.description);
		bean.setPriority(this.priority);
		bean.setContributeLevel(this.contributeLevel);
        bean.setDisplay(this.display); 
		bean.setTplContent(this.tplContent);
		bean.setHasChild(false);
		bean.setParam1(this.param1);
		bean.setParam2(this.param2);
		bean.setParam3(this.param3);	
		if(this.outerUrl != null){
			bean.setOuterUrl(this.outerUrl);	
		}
	}
	
	private ArticleServ  articleServ;	
	private CmsChannelServ cmsChannelServ;
	private ChnlModelServ chnlModelServ;
	private CmsChannel bean;
	private CmsChannel parent;
	private String message;
	private int start;
	private int limit;	
	
	private long modelId;
    private Long parentId;
	private String name;
	private String path;
	private String tplIndex; 
	private String tplContent; 
	private String title;
	private String keywords;
	private String description;
	private Long priority;
	private Long contributeLevel;
	private Boolean  display;
	private Boolean mainshow;
	private String param1;
	private String param2;
	private String param3;
	private String outerUrl;
	
	private String node;	

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNode() {
		return node;
	}

	public void setNode(String node) {
		this.node = node;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	protected Object getBeanInput() {	
		return null;
	}

	@SuppressWarnings("unchecked")
	protected BaseServer getManager() {		
		return null;
	}
	
	public CmsChannel getBean() {
		return bean;
	}

	public void setBean(CmsChannel bean) {
		this.bean = bean;
	}

	public ChnlModelServ getChnlModelServ() {
		return chnlModelServ;
	}

	public void setChnlModelServ(ChnlModelServ chnlModelServ) {
		this.chnlModelServ = chnlModelServ;
	}

	public CmsChannelServ getCmsChannelServ() {
		return cmsChannelServ;
	}

	public void setCmsChannelServ(CmsChannelServ cmsChannelServ) {
		this.cmsChannelServ = cmsChannelServ;
	}

	public CmsChannel getParent() {
		return parent;
	}

	public void setParent(CmsChannel parent) {
		this.parent = parent;
	}

	public long getModelId() {
		return modelId;
	}

	public void setModelId(long modelId) {
		this.modelId = modelId;
	}

	public Long getContributeLevel() {
		return contributeLevel;
	}

	public void setContributeLevel(Long contributeLevel) {
		this.contributeLevel = contributeLevel;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Boolean getDisplay() {
		return display;
	}

	public void setDisplay(Boolean display) {
		this.display = display;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getParam1() {
		return param1;
	}

	public void setParam1(String param1) {
		this.param1 = param1;
	}

	public String getParam2() {
		return param2;
	}

	public void setParam2(String param2) {
		this.param2 = param2;
	}

	public String getParam3() {
		return param3;
	}

	public void setParam3(String param3) {
		this.param3 = param3;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Long getPriority() {
		return priority;
	}

	public void setPriority(Long priority) {
		this.priority = priority;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getTplContent() {
		return tplContent;
	}

	public void setTplContent(String tplContent) {
		this.tplContent = tplContent;
	}

	public String getTplIndex() {
		return tplIndex;
	}

	public void setTplIndex(String tplIndex) {
		this.tplIndex = tplIndex;
	}

	public long getChanId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}
	
	public Long getParentId() {
		return parentId;
	}

	public String getOuterUrl() {
		return outerUrl;
	}

	public void setOuterUrl(String outerUrl) {
		this.outerUrl = outerUrl;
	}

	public Boolean getMainshow() {
		return mainshow;
	}

	public void setMainshow(Boolean mainshow) {
		this.mainshow = mainshow;
	}

	public ArticleServ getArticleServ() {
		return articleServ;
	}

	public void setArticleServ(ArticleServ articleServ) {
		this.articleServ = articleServ;
	}
}
